import axios from 'axios'
import store from '../store'
const sessionToken = store.state.user ? store.state.user.accessToken : ''
const token = sessionToken || ''

export function post (url, params) {
  return new Promise((resolve, reject) => {
    axios.post(url, params, { headers: { 'Content-Type': 'application/json;charset=utf8', 'Authorization': token } }).then(
      res => {
        resolve(res.data)
      },
      err => {
        reject(err.data)
      }
    )
      .catch(err => {
        reject(err.data)
      })
  }).catch((e) => {})
}

export function postfile (url, params) {
  return new Promise((resolve, reject) => {
    axios.post(url, params, { headers: { 'Content-Type': 'application/json;charset=utf8', 'Authorization': token } }).then(
      res => {
        resolve(res.data)
      },
      err => {
        reject(err.data)
      }
    )
      .catch(err => {
        reject(err.data)
      })
  }).catch((e) => {})
}

export function put (url, params) {
  return new Promise((resolve, reject) => {
    axios.put(url, params, { headers: { 'Content-Type': 'application/json;charset=utf8', 'Authorization': token } }).then(
      res => {
        resolve(res.data)
      },
      err => {
        reject(err.data)
      }
    )
      .catch(err => {
        reject(err.data)
      })
  }).catch((e) => {})
}

export function get (url, params) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params

    }, { headers: { 'Content-Type': 'application/json;charset=utf8', 'Authorization': token } })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  }).catch((e) => {})
}

export function del (url, params) {
  return new Promise((resolve, reject) => {
    axios.delete(url, {
      params: params
    })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  }).catch((e) => {})
}

export default {
  // 接口测试
  getErrcode (params) { return get('/api/code/doCode', params) }

}
